package com.framework2.popupwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.library.utils.ScreenUtils;
import com.yaxin.yyt.R;

/**
 * 自定义popupwindow
 *
 * @author YobertJomi
 * className GoHomepagePopupwindow
 * created at  2017/3/17  11:24
 */

public class GoHomepagePopupwindow extends PopupWindow {
    private Context context;
    private View viewRoot;
    private RelativeLayout messageLayout;
    private ImageView iv_mark;
    private TextView tv_goback_homepage;

    public GoHomepagePopupwindow(Context context) {
        super(context);
        this.context = context;
        initView();
        initStyle();
    }

    private void initView() {
        viewRoot = LayoutInflater.from(context).inflate(
                R.layout.popwindow_goback_homepage, null);
        messageLayout = (RelativeLayout) viewRoot.findViewById(R.id.messageLayout);
        iv_mark = (ImageView) viewRoot.findViewById(R.id.iv_mark);
        tv_goback_homepage = (TextView) viewRoot.findViewById(R.id.tv_goback_homepage);
        setWidth(ScreenUtils.getInstance().dip2px(context, 100));
        setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        setContentView(viewRoot);
    }

    private void initStyle() {
        // 设置整个popupwindow的弹出，收起方式
        setAnimationStyle(R.style.popWindowAnim);
        // 需要设置一下此参数，点击外边可消失
        setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        setFocusable(true);
    }

    /**
     * @param isShowRightMarkIcon 是否显示右边红点
     * @return this
     */
    public GoHomepagePopupwindow showRightMarkIcon(boolean isShowRightMarkIcon) {
        iv_mark.setVisibility(isShowRightMarkIcon ? View.VISIBLE : View.GONE);
        return this;
    }

    public GoHomepagePopupwindow setOnClickGobackHomeListener(final OnClickGobackHomeListener
                                                                      onClickGobackHomeListener) {
        if (null != onClickGobackHomeListener) {
            tv_goback_homepage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickGobackHomeListener.onClickGobackHome();
                }
            });
        }
        return this;
    }

    public GoHomepagePopupwindow setOnClickOpenMessageListener(final OnClickOpenMessageListener
                                                                       onClickOpenMessageListener) {
        if (null != onClickOpenMessageListener) {
            messageLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickOpenMessageListener.onClickOpenMessage();
                }
            });
        }
        return this;
    }

    public interface OnClickGobackHomeListener {
        void onClickGobackHome();
    }

    public interface OnClickOpenMessageListener {
        void onClickOpenMessage();
    }
}
